module planegame{

    export class Game extends egret.DisplayObjectContainer {

        private timer: egret.Timer;
        
   //     public bulletArray: Array<egret.Sprite>;
   //     public enemyBulletArray: Array<egret.Sprite>;
        private enemyArray: Array<Plane>;

        // 敌军飞机游泳池
        private enemyBuffer: Array<Plane>;
        
        // 我军飞机游泳池
        private planeBuffer: Array<Plane>;

        private planeFactory: PlaneFactory;

        public constructor(stageW :number, stageH :number) {
            super();
            this.init(stageW, stageH);
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        }

        private init(stageW :number, stageH :number) {

            this.planeFactory = new PlaneFactory();
//            this.bulletArray = new Array<egret.Sprite>();
//            this.enemyBulletArray = new Array<egret.Sprite>();
            this.enemyArray = new Array<Plane>();
            this.enemyBuffer = new Array<Plane>();
            this.planeBuffer = new Array<Plane>();


            let background = BulletFactory.createBackGround("back_jpg");
            this.addChildAt(background, 0);
            background.width = stageW;
            background.height = stageH;
            background.setFillStage(stageW, stageH);
            background.startScroll();

            // 保存屏幕宽高
            GameUtils.instance().setStageWidth(stageW);
            GameUtils.instance().setStageHeight(stageH);
            
            this.addPlane();
            this.addEnemy();
            this.cacheAsBitmap = true;
        }

        private addPlane() {
            let plane = this.planeFactory.getPlane("plane_png", "plane");
            this.addChildAt(plane, 1);
            plane.width = 60;
            plane.height = 40;
            plane.x = 100;
            plane.y = 300;
            plane.anchorOffsetX = 30;
            plane.anchorOffsetY = 20;
            plane.rotation = 90;
            plane.setBullet("bullet_png", 1, this);

            plane.setTimerDelay(100);

            // 飞机无敌
            plane.canHit = false;
        }

        private addEnemy() {
            let enemyPlane = this.planeFactory.getEnemy("plane_png", "enemy");
            this.addChild(enemyPlane);
            this.enemyArray.push(enemyPlane);
            enemyPlane.width = 60;
            enemyPlane.height = 40;
            enemyPlane.x = 800;
            enemyPlane.y = Math.random()*300;
            enemyPlane.anchorOffsetX = 30;
            enemyPlane.anchorOffsetY = 20;
            enemyPlane.rotation = 270;
            enemyPlane.setBullet("bullet_png", 3, this);

            enemyPlane.setTimerDelay(2000);

            enemyPlane.canHit = false;

            egret.Tween.get(enemyPlane).to({x:enemyPlane.x-1000, y:enemyPlane.y}, 10000).call(function() {
                let index = this.enemyArray.indexOf(enemyPlane);
                if(index != -1) {
                    this.enemyArray.splice(index, 1);
                    this.removeChild(enemyPlane);
                }
            }, this);

        }

        private onTimeUpdate():void{
            //egret.log("onTimeUpdate");
            this.addEnemy();
        }

        private onTimeComplete():void{
            
        }

        private enterFrame(): void {
            let plane:Plane = <Plane>this.getChildByName("plane");

            /* 更新子弹位置 */
            // 我军子弹
            plane.bullet.updateFrame();
            // 敌军子弹
            this.planeFactory.enemyArray.forEach(function(vEnemy, i, a) {
                vEnemy.bullet.updateFrame();
            }, this);

            /* 碰撞检测部分 */

            // 我军子弹和敌军相撞 全部消失
            plane.bulletArray.forEach(function(vBullet, i, a) {
                // 判断是否碰撞
                this.enemyArray.forEach(function(vEnemy, i2, a2) {
                    if(vEnemy.canHit) {
                        if(this.isHit(vBullet, vEnemy)) {
                            let indexBullet = this.bulletArray.indexOf(vBullet);
                            if(indexBullet != -1) {
                                this.bulletArray.splice(indexBullet, 1);
                                this.removeChild(vBullet);
                            }
                            
                            let indexEnemy = this.enemyArray.indexOf(vEnemy);
                            if(indexEnemy != -1) {
                                this.enemyArray.splice(indexEnemy, 1);
                                this.removeChild(vEnemy);
                            }
                        
                        }
                    }
                    
                }, this);
            }, this);

            if(plane.canHit) {
                this.planeFactory.enemyArray.forEach(function(vEnemy, i2, a2) {
                    // 敌军
                    vEnemy.bulletArray.forEach(function(vBullet, i, a) {
                        // 子弹
                        if(this.isHit(plane, vBullet)) {
                            // 删除我军飞机
                            this.removeChildAt(1);
    /*
                            // 播放动画
                            var texture = RES.getRes("newParticle_png");  
                            var config = RES.getRes("newParticle_json");
                            var particleSys = new particle.GravityParticleSystem(texture,config);

                            var timer:egret.Timer = new egret.Timer(2000,1);
                            timer.addEventListener(egret.TimerEvent.TIMER, function() {
                                particleSys.stop();
                                this.removeChild(particleSys);
                            }, this);
                            timer.start();
                            this.addChild(particleSys);
                            particleSys.start();

                            */
                            this.addPlane();
                        }
                    }, this);

                }, this);

            }

        }

        private isHit(obj1:egret.DisplayObjectContainer, obj2:egret.DisplayObjectContainer): boolean {
            let rect1:egret.Rectangle = obj1.getBounds();
            rect1.x = obj1.x - obj1.anchorOffsetX;
            rect1.y = obj1.y - obj1.anchorOffsetY;
            let rect2:egret.Rectangle = obj2.getBounds();
            rect2.x = obj2.x - obj2.anchorOffsetX;
            rect2.y = obj2.y - obj2.anchorOffsetY;

            return rect1.intersects(rect2);
        }

        private onAddToStage(event: egret.Event) {
            this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.end, this);

            // chumo
            this.touchEnabled = true;
            this.addEventListener( egret.TouchEvent.TOUCH_MOVE, this.onTouch, this );

            // timer
            var timer:egret.Timer = new egret.Timer(500,0);
            this.timer = timer;
            timer.addEventListener(egret.TimerEvent.TIMER, this.onTimeUpdate, this);
            this.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.onTimeComplete, this);
            timer.start();

            // frame
            this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
        }

        // chumo
        private onTouch( evt:egret.TouchEvent ) {
            //egret.log("touch begin:"+evt.stageX);
            let x = evt.stageX;
            let y = evt.stageY;
            let plane = this.getChildByName("plane");
            plane.x = x;
            plane.y = y;

            //this.dispatchEvent(new egret.Event("changeStageStartMenu"));

            //egret.Tween.get( plane ).to( {x:x,y:y}, 300, egret.Ease.sineIn );
        }

        // recycle
        public end() {
            this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.end, this);
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

            // chumo
            this.touchEnabled = false;
            if(this.hasEventListener(egret.TouchEvent.TOUCH_TAP)) {
                this.removeEventListener( egret.TouchEvent.TOUCH_TAP, this.onTouch, this );
            }

            // timer
            if(this.timer.running) {
                this.timer.stop();
            }
            if(this.timer.hasEventListener(egret.TimerEvent.TIMER)) {
                this.timer.removeEventListener(egret.TimerEvent.TIMER, this.onTimeUpdate, this);
            }
            if(this.hasEventListener(egret.TimerEvent.TIMER_COMPLETE)) {
                this.removeEventListener(egret.TimerEvent.TIMER_COMPLETE, this.onTimeComplete, this);
            }
            

            // frame
            if(this.hasEventListener(egret.Event.ENTER_FRAME)) {
                this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
            }
            
        }
    }

}